<template>
    <div>
        <a-form layout="inline"
                :model="searchData"
                style="margin: 10px 0">
            <a-form-item>
                <a-input v-model:value="searchData.name" placeholder="名称关键字"></a-input>
            </a-form-item>
            <a-form-item>
                <a-button type="primary" @click="load">搜索</a-button>
            </a-form-item>
            <a-form-item>
                <a-button type="default" @click="rest">重置</a-button>
            </a-form-item>
        </a-form>
        <a-button type="primary" style="margin-bottom: 10px" @click="openModal({})">新增</a-button>
        <a-table :dataSource="contentCategoryList"
                 :columns="columns"
                 :pagination="pagination"
                 @change="handlerChange"
                 bordered>
            <template v-slot:action="{text,record}">
                <a-space>
                    <a-button type="primary" @click="openModal(record)">编辑</a-button>
                    <a-popconfirm
                            title="删除数据无法恢复，请确认是否删除？"
                            ok-text="是"
                            cancel-text="否"
                            @confirm="okDelete(record.id)"
                    >
                        <a-button type="danger">删除</a-button>
                    </a-popconfirm>
                </a-space>
            </template>
        </a-table>
        <a-modal v-model:visible="visible" title="新增或编辑" @ok="handleOk">
            <a-form v-model="contentCategory"
                    :label-col="{ span: 8 }">
                <a-form-item label="广告类别名称">
                    <a-input v-model:value="contentCategory.name"></a-input>
                </a-form-item>
            </a-form>
        </a-modal>
    </div>
</template>

<script lang="ts">
    import {ref, onMounted} from 'vue'
    import axios from 'axios'
    import {message} from 'ant-design-vue';
    export default {
        name: "admin-contentCategory",
        setup() {
            const columns = [
                {
                    title: 'ID',
                    dataIndex: 'id',
                    key: 'id',
                    align: 'center'
                },
                {
                    title: '广告类别',
                    dataIndex: 'name',
                    key: 'name',
                    align: 'center'
                },
                {
                    title: '操作',
                    key: 'action',
                    slots: {customRender: 'action'},
                    align: 'center'
                }
            ]

            const contentCategory = ref({})
            const contentCategoryList = ref()
            const searchData = ref({
                name: null,
                
            })
            const pagination = ref({
                current: 1,
                pageSize: 10,
                total: 0
            })

            const visible = ref(false);

            const load = () => {
                axios.get('/mall-contents/contentCategory/list', {
                    params: {
                        pageNum: pagination.value.current,
                        pageSize: pagination.value.pageSize,
                        name: searchData.value.name
                    }
                }).then(rs => {
                    console.log("查找结果：", rs)
                    contentCategoryList.value = rs.data.data.list
                    pagination.value.total = rs.data.data.total
                })
            }

            const rest = () => {
                searchData.value = {name:null}
                load()
            }

            const handlerChange = (pageInfo:any) => {
                pagination.value = pageInfo
                load()
            }

            const openModal = (obj:any) => {
                console.log("模态框参数：", obj)
                visible.value = true;
                contentCategory.value = JSON.parse(JSON.stringify(obj))
            }

            const handleOk = () => {
                axios.post('/mall-contents/contentCategory/saveOrUpdate', contentCategory.value).then(rs => {
                    console.log("新增或修改结果：", rs)
                    if (rs.data.code == 200) {
                        visible.value = false
                        message.success("操作成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            const okDelete = (id:number) => {
                console.log("要删除的ID：" + id)
                axios.delete('/mall-contents/contentCategory/deleteById/' + id).then(rs => {
                    if (rs.data.code == 200) {
                        message.success("删除成功")
                        load()
                    } else {
                        message.error(rs.data.message)
                    }
                })
            }

            onMounted(() => {
                load()
            })
            return {
                columns,
                contentCategory,
                contentCategoryList,
                pagination,
                handlerChange,
                searchData,
                load,
                rest,
                openModal,
                visible,
                handleOk,
                okDelete
            }
        }
    }
</script>

<style scoped>

</style>